<template>
  <div>
    <!-- 1.页面布局 -->
    <el-card class="box-card">
      <!-- 1.1头部 -->
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addvisible = true"
          >添加分类</el-button
        >
      </div>
      <!-- 1.2表格 -->
      <el-table :data="list" style="width: 100%">
        <el-table-column type="index" label="序号" width="100px">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="obj">
            <el-button type="primary" @click="modify(obj.row)">修改</el-button>
            <el-button type="danger" @click="del(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 2.弹框 -->
    <el-dialog :title="dialogtext.title" :visible.sync="addvisible" @close="doClose">
      <el-form label-position="left" label-width="80px" :model="newArt" :rules="newArtRules" ref="newArtRef" >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input autocomplete="off" v-model="newArt.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input autocomplete="off" v-model="newArt.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$refs.newArtRef.resetFields()">取 消</el-button>
        <el-button type="primary" @click="addArt">{{ dialogtext.btn }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "ArtCate",
  //1
  data() {
    return {
      //1.1分类列表
      list: [],
      //1.2弹框布尔值
      addvisible: false,
      //1.3添加分类
      newArt: {
        cate_name: "",
        cate_alias: "",
      },
      //1.4 正则
      newArtRules: {
        cate_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            pattern: /^\S{1,10}$/,
            message: "分类名称必须是1-10位的非空字符串",
            trigger: "blur",
          },
        ],
        cate_alias: [
          { required: true, message: "分类别名不能为空", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: "分类别名必须是1-15位的字母数字",
            trigger: "blur",
          },
        ],
      },
      //1.5 dialog对话框文本
      dialogtext: {
        title: "新增分类",
        btn: "新增",
      },
    };
  },
  //2.点击事件
  methods: {
    //2.1 封装列表刷新ajax
    async initArtCate() {
      const { data: res } = await this.$axios.get("/my/cate/list");
      console.log(res);
      this.list = res.data;
    },
    //2.2 新增分类ajax
    addArt() {
      //(1)检查表单是否校验成功
      this.$refs.newArtRef.validate(async (valid) => {
        // valid 就是表单验证的结果，如果是true，表示通过了
        console.log(valid);
        //(2)如果成功,则发送注册ajax
        if (valid) {
          //解构赋值 const {属性名:属性值}=对象名
          //逻辑判断
          let promise=this.$axios.post("/my/cate/add",this.newArt);
          if (this.dialogtext.btn=='编辑') {
            promise=this.$axios.put("/my/cate/info",this.newArt);
          } 
          const { data: res } = await promise;
          console.log(res);
          if (res.code === 0) {
            this.$message.success(res.message);
            this.newArt.cate_name = "";
            this.newArt.cate_alias = "";
            this.addvisible = false;
            this.initArtCate();
          } else {
            this.$message.warning(res.message);
          }
        }
      });
    },
    //2.3删除分类ajax
    async del(id) {
      console.log(id);
      //(1)弹出确认框
      const confirm = await this.$confirm(
        "此操作将永久删除该分类, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => {});
      //(2)点击确定发送ajax请求
      if (confirm) {
        const { data: res } = await this.$axios.delete("/my/cate/del", {
          params: { id },
        });
        console.log(res);
        if (res.code === 0) {
          this.$message.success("删除分类成功！");
          //重新加载分类列表
          this.initArtCate();
        } else {
          this.$message.error(res.message);
        }
      }
    },
    //2.4 编辑分类
    async modify(item) {
      console.log(item);
      //(1)修改对话框文本
      this.dialogtext = {
        title: "修改分类",
        btn: "编辑",
      };
      //（2）编辑显示当前编辑内容
      this.newArt = { ...item };
      //弹出对话框
      this.addvisible = true;
    },
    //2.5对话框复位
    doClose() {
      this.dialogtext = {
        title: "新增分类",
        btn: "新增",
      };
      this.newArt = { cate_name: "", cate_alias: "" };
    },
  },
  //页面加载就发送axios
  created() {
    /* 
    1.如果一个ajax，整个页面只加载一次，那么写在created勾子里 
    2.如果一个ajax，整个页面多次加载，写在methods里面，created里调用一次
    */
    this.initArtCate();
  },
};
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
